package daily.may0528;

//1.创建一个Runnable接口的实现类
class RunnableImpl implements Runnable {
    //2.在实现类中重写Runnable接口的run方法,设置线程任务
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "正在执行");
    }
}

public class RunnableTest {
    public static void main(String[] args) {
        //3.创建一个Runnable接口的实现类对象
        RunnableImpl runnable = new RunnableImpl();
        //4.创建Thread类对象,构造方法中传递Runnable接口的实现类对象
        Thread thread = new Thread(runnable);
        //5.调用Thread对象的start方法,开启新的线程执行run方法
        thread.start();//Thread-0正在执行

        //通过匿名内部类和匿名对象
        Thread helloWorld = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello World");
            }
        });
        helloWorld.start();//Hello World

        //通过Lambda表达式
        Thread helloLambda = new Thread(() -> System.out.println("Hello Lambda"));
        helloLambda.start();//Hello Lambda
    }
}
